//
//  MYXReleaseViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/27.
//  Copyright © 2017年 hbjApple. All rights reserved.
//我的发布
#import "UIView+Common.h"
#import "MYXReleaseViewController.h"
#import "MYXNewReleaseTopViewCell.h"
#import "MYXReleasePhotoViewCell.h"
#import "MYXmypublishModel.h"
#import "MYXmypublishViewModel.h"
#import "MYXcontentCell.h"
#import "MYXLukcyMoneyDetailViewController.h"
#import "UIView+Additions.h"
#import "MXYLuckymoneyViewModel.h"
#import "MYXpublishDealViewController.h"
#import "MYXDetailViewModel.h"
#import "MYXNewReleaseTopCell.h"
#import "MYY_walletNavView.h"
#import "MYXReleaseTopView.h"
#import "MYCIndexCardViewController.h"
#import "MYXMyInformationController.h"

static NSString *const MYXNewReleaseTopViewCellIdentifier = @"MYXNewReleaseTopViewCell";
//static NSString *const MYXNewReleaseTopViewCellIdentifier = @"MYXNewReleaseTopCell";
static NSString *const MYXReleasePhotoViewCellIdentifier = @"MYXReleasePhotoViewCell";
static NSString *const MYXcontentCellIdentifier = @"MYXcontentCell";
@interface MYXReleaseViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>{
    MYXNewReleaseTopViewCell *MYXNewReleaseTopViewCe ;
    // MYXNewReleaseTopCell * MYXNewReleaseTopCe;
    MYXReleasePhotoViewCell *MYXReleasePhotoViewCe ;
    MYXcontentCell *MYXcontentCe;
    MYXmypublishViewModel *MYXmypublishViewMod;
    CGFloat lastOriginalY;
    MXYLuckymoneyViewModel *model;
    MYXDetailViewModel *MYXDetailViewMod;
}
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property(nonatomic, assign) int pageStart;
@property(nonatomic, assign) int type;//判断是否刷新
@property(nonatomic,strong)MYY_walletNavView * MYreleaseView;

@end

@implementation MYXReleaseViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark -PrivateMethod
- (void)backAct{
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageStart=1;
    if (MYXmypublishViewMod == nil) {
        MYXmypublishViewMod = [[MYXmypublishViewModel alloc] init];
    }
    if (model == nil) {
        model = [[MXYLuckymoneyViewModel alloc] init];
    }
    if (MYXDetailViewMod == nil){
        MYXDetailViewMod = [[MYXDetailViewModel alloc] init];
    }
    AdjustsScrollViewInsetNever(self, self.tableView);
    //  [self.tableView.mj_header beginRefreshing];
    //  [self initNavigation];
    [self loadNewData];
    [self createUI];
}

#pragma mark - Navigation
-(void)initNavigation{
    WS(weakSelf);
    _MYreleaseView = [[MYY_walletNavView alloc]init];
    self.MYreleaseView.frame = CGRectMake(0, 0, _tableView.frame.size.width,SafeAreaTopHeight);
    _MYreleaseView.nameLable.text = @"我的发布";
    [self.view addSubview:_MYreleaseView];
    [self.MYreleaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@(SafeAreaTopHeight));
    }];
    [self.MYreleaseView.backButton setImage:[UIImage imageNamed:@"kai"] forState:UIControlStateNormal];
    self.MYreleaseView.backButton =^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    self.MYreleaseView.rightButton =^{
    };
}


#pragma mark - UI

- (void)createUI
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(0));
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self.tableView registerNib:[UINib nibWithNibName:MYXNewReleaseTopViewCellIdentifier bundle:nil] forCellReuseIdentifier:MYXNewReleaseTopViewCellIdentifier];
    //[self.tableView registerClass:[MYXNewReleaseTopCell class] forCellReuseIdentifier:MYXNewReleaseTopViewCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:MYXReleasePhotoViewCellIdentifier bundle:nil] forCellReuseIdentifier:MYXReleasePhotoViewCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:MYXcontentCellIdentifier bundle:nil] forCellReuseIdentifier:MYXcontentCellIdentifier];
    [self initNavigation];
    
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -SafeAreaTopHeight, WIDTH, HEIGHT+20) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        WS(weakSelf)
        //        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //            [weakSelf loadNewData];
        //        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf loadMorewData];
            
        }];
        
    }
    return _tableView;
}
#pragma mark - request

- (void)loadNewData
{
    self.pageStart = 1;
    [self.tableView.mj_header beginRefreshing];
    [self.dataArr removeAllObjects];
    [self mypublish:self.pageStart];
    
}
- (void)loadMorewData
{
    if (self.tableView.mj_header.isRefreshing) {
        return;
    }
    self.pageStart ++;
    [self mypublish:self.pageStart];
}
#pragma mark - tableview datasource and delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==1) {
        return MYXmypublishViewMod.publishArray.count;
    }
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf)
    if (indexPath.section==0) {
        MYXNewReleaseTopViewCe = [tableView dequeueReusableCellWithIdentifier:MYXNewReleaseTopViewCellIdentifier forIndexPath:indexPath];
        MYXNewReleaseTopViewCe.selectionStyle = UITableViewCellSelectionStyleNone;
        return MYXNewReleaseTopViewCe;
    }
    if (MYXmypublishViewMod.publishArray.count > 0) {
        MYXmypublishModel *MYXmypublishMo =MYXmypublishViewMod.publishArray[indexPath.row];
        if (MYXmypublishMo.file.length>0) {
            MYXReleasePhotoViewCe = [tableView dequeueReusableCellWithIdentifier:MYXReleasePhotoViewCellIdentifier forIndexPath:indexPath];
            MYXReleasePhotoViewCe.selectionStyle = UITableViewCellSelectionStyleNone;
            MYXReleasePhotoViewCe.MYXmypublishMo = MYXmypublishMo;
            return MYXReleasePhotoViewCe;
        }else{
            MYXcontentCe = [tableView dequeueReusableCellWithIdentifier:MYXcontentCellIdentifier forIndexPath:indexPath];
            MYXcontentCe.selectionStyle = UITableViewCellSelectionStyleNone;
            MYXcontentCe.MYXmypublishMo = MYXmypublishMo;
            return MYXcontentCe;
        }
    }
    
    return nil;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 269;
    }
    if (MYXmypublishViewMod.publishArray.count > 0) {
        MYXmypublishModel *MYXmypublishMo =MYXmypublishViewMod.publishArray[indexPath.row];
        return MYXmypublishMo.height;
    }
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        MYXMyInformationController * vc = [[MYXMyInformationController alloc] init];
        vc.userID = [MYXMemberMdel sharedInstance].userinfo.userid;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        if (MYXmypublishViewMod.publishArray.count > 0) {
            MYXmypublishModel *MYXmypublishMo =MYXmypublishViewMod.publishArray[indexPath.row];
            [self initRed:MYXmypublishMo.blish_id];
        }
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // NSLog(@"scrollview offset %f",scrollView.contentOffset.y);
    lastOriginalY = scrollView.contentOffset.y;
    CGFloat consTop;
    if (lastOriginalY < 0) {
        consTop  = - lastOriginalY;
    }else{
        consTop = 0;
    }
    
    CGFloat alpha = (consTop+360)/360;
    alpha=fabs(alpha);
    alpha=fabs(1-alpha);
    
    alpha=alpha<0.2? 0:alpha-0.2;
    
    if (MYXNewReleaseTopViewCe) {
        MYXNewReleaseTopViewCe.topCons.constant = -consTop;
        //MYXRedListHeadCellId.bgImageView.alpha =alpha;
    }
}
#pragma mark -  我的发布接口


-(void)mypublish:(int)page{
    if (page==0) {
        [MYXmypublishViewMod.publishArray removeAllObjects];
    }
    [MYXmypublishViewMod loadmypublish:page WithResult:^(BOOL isSucess) {
        if (MYXmypublishViewMod.publishArray.count==0) {
            [self.tableView configBlankPage:BlankReleaseView hasData:NO hasError:NO reloadButtonBlock:nil];
            self.tableView.mj_footer.hidden = YES;
        }
        if (isSucess) {
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }else{
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}


-(void)initRed:(NSString *)redID{
    model.LuckyMoneyId = redID;
    [model getMoneyDetailWithIsPullup:NO Completion:^(BOOL success, NSString *message) {
        [SVProgressHUD dismiss];
        if (success) {
            MYXpublishDealViewController *detailVC = [[MYXpublishDealViewController alloc] init];
            detailVC.model = model;
            detailVC.detailviewModel = model.viewModel;
            self.navigationController.navigationBarHidden = YES;
            [self.navigationController pushViewController:detailVC animated: YES];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

